//
//  SetRequest.m
//  MoveCar
//
//  Created by dengbb on 15/3/25.
//
//

#import "SetRequest.h"
#import "AFNetworking.h"


@implementation SetRequest

+(SetRequest*)sharedInstance{
    
    static dispatch_once_t onceToken;
    static SetRequest* sharedInstance;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[SetRequest alloc]init];
    });
    
    return sharedInstance;
    
}
-(void )getRequestWithURL: (NSString *)url postParems:(NSMutableDictionary *)postParems content:(void(^)(NetWorkModel *model))requestResult
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:url parameters:postParems success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NetWorkModel* model =[[NetWorkModel alloc]init];
        model.responseDic = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        model.responeArray = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        requestResult(model);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NetWorkModel* model =[[NetWorkModel alloc]init];
        model.error = error;
        requestResult(model);
        
    }];
    
}
-(void )postRequestWithURL: (NSString *)url postParems:(NSMutableDictionary *)postParems content:(void(^)(NetWorkModel *model))requestResult
{
    

    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];

    
        m.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        m.responseSerializer = [AFHTTPResponseSerializer serializer];
        [m POST:url parameters:postParems success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NetWorkModel* model =[[NetWorkModel alloc]init];
         model.responseDic = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        model.responeArray = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        requestResult(model);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NetWorkModel* model =[[NetWorkModel alloc]init];
        model.error = error;
        requestResult(model);
    }];
    
}
-(void )postRequestWithURLNoloading: (NSString *)url postParems:(NSMutableDictionary *)postParems content:(void(^)(NetWorkModel *model))requestResult
{

    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    
    [m POST:url parameters:postParems success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NetWorkModel* model =[[NetWorkModel alloc]init];
        model.responseDic = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        model.responeArray = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        requestResult(model);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NetWorkModel* model =[[NetWorkModel alloc]init];
        model.error = error;
        requestResult(model);
    }];
    
}
// 改方法不要轻易动 影响较大
-(void )postRequestWithURL: (NSString *)url postParems:(NSMutableDictionary *)postParems withHeader:(NSString *)key content:(void(^)(NetWorkModel *model))requestResult
{
    
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    [m.requestSerializer setValue:key forHTTPHeaderField:@"加密头内容"];

    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m POST:url parameters:postParems success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NetWorkModel* model =[[NetWorkModel alloc]init];
        model.responseDic = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
         model.responeArray = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        model.operation = operation;
        requestResult(model);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NetWorkModel* model =[[NetWorkModel alloc]init];
        model.error = error;
        requestResult(model);
    }];

}
-(void)postRequestWithURLWithImg: (NSString *)url postParems:(NSMutableDictionary *)postParems withURLPath:(NSMutableArray *)paths  content:(void(^)(NetWorkModel *model))requestResult{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 600;
    NSURL *filePath = [NSURL fileURLWithPath:[paths firstObject]];
    [manager POST:url parameters:postParems constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:filePath name:@"file" error:nil];
//        [formData appendPartWithFileURL:filePath name:@"back_images[]" error:nil];
        for (int i = 1; i <= (int)paths.count / 2; i++) {
            NSURL *backImagePath = [NSURL fileURLWithPath:paths[i]];
            [formData appendPartWithFileURL:backImagePath name:@"back_images[]" error:nil];
        }
        for (int i = (int)paths.count / 2 + 1; i < paths.count; i++) {
            NSURL *backImagePath = [NSURL fileURLWithPath:paths[i]];
            [formData appendPartWithFileURL:backImagePath name:@"back_blank_images[]" error:nil];
        }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NetWorkModel* model =[[NetWorkModel alloc]init];
        model.responseDic = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        model.responeArray = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:NSJSONReadingMutableContainers error:nil];
        model.operation = operation;
        requestResult(model);
        NSLog(@"Success: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NetWorkModel* model =[[NetWorkModel alloc]init];
        model.error = error;
        requestResult(model);
        NSLog(@"Error: %@", error);
    }];
    
    

     /*
     AFHTTPRequestOperationManager *ma = [AFHTTPRequestOperationManager manager];
     ma.responseSerializer.acceptableContentTypes =[NSSet setWithObject:@"text/html"];
     
     [ma POST:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
     
     NSData *data = UIImageJPEGRepresentation(rimage, 0.5);
     
     [formData appendPartWithFileData:data name:currentTag fileName:[currentTag stringByAppendingString:@".jpg"] mimeType:@"image/jpg"];
     
     } success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@" 结果 ===== %@",responseObject);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     }];
     */
}

@end
